package cn.fl.brcrlit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.brcrlit.condition.PrjBrCrLimRecQC;
import cn.fl.brcrlit.dto.PrjBrCrLimRecDTO;
import cn.fl.brcrlit.service.IBrCrLitApiService;
import cn.fl.brcrlit.service.IPrjBrCrLimRecService;
import cn.fl.brcrlit.transform.PrjBrCrLimRecVOTransform;
import cn.fl.brcrlit.vo.PrjBrCrLimRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.dto.PrjPrdDtlDTO;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.vo.PrjPrdPaySchMVO;

/**
 * @Description:  客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjBrCrLimRecServiceImpl implements IPrjBrCrLimRecService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrjBrCrLimRecServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IBrCrLitApiService brCrLitApiService;
	
	@Autowired
	private IProjectApiService projectApiService;
	
	@Autowired
	private IPrjPrdPaySchMService prjPrdPaySchMService;
	
	/**
	 * 字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public PrjBrCrLimRecVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		PrjBrCrLimRecDTO dto = null;//this.自定义ApiService.selectById(id);
		return PrjBrCrLimRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjBrCrLimRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.prjBrCrLimRecApiService.add(PrjBrCrLimRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.prjBrCrLimRecApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PrjBrCrLimRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.prjBrCrLimRecApiService.modify(PrjBrCrLimRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.brCrLitApiService.searchLimRecListPage(page);
		List<PrjBrCrLimRecDTO> list = (List<PrjBrCrLimRecDTO>) page.getResultList();
		if(CollectionUtils.isNotEmpty(list)){
			for(PrjBrCrLimRecDTO prjBrCrLimRecDTO :list){
				//额度变化类型
				Map<String, String> limTypCdNmMap = this.sysDictionaryApiService.searchGroupToMap("11000", "0000", DictionaryConstant.CODE_PRJ_AMT_CHG_TYPE_CD) ; //额度变化类型名称
				if(limTypCdNmMap.containsKey(prjBrCrLimRecDTO.getLimTypCd())){
					prjBrCrLimRecDTO.setLimTypCdNm(limTypCdNmMap.get(prjBrCrLimRecDTO.getLimTypCd()));
				}
				//项目名称
				Long prjId = prjBrCrLimRecDTO.getPrjId();
				PrjBscInfoDTO prjBscInfoDTO = this.projectApiService.selectById(prjId);
				if(null!=prjBscInfoDTO){
					prjBrCrLimRecDTO.setPrjNm(prjBscInfoDTO.getPrjNm());
				}
				//支付表编号
				Long paySchId = prjBrCrLimRecDTO.getPaySchId();
				PrjPrdPaySchMVO prjPrdPaySchMVO = this.prjPrdPaySchMService.selectById(paySchId);
				if(null!=prjPrdPaySchMVO){
					prjBrCrLimRecDTO.setPaySchNo(prjPrdPaySchMVO.getPaySchNo());
				}
				
				PrjPrdDtlQueryCondition prjPrdDtlQc = new PrjPrdDtlQueryCondition();
				prjPrdDtlQc.setPaySchId(paySchId);
				List<PrjPrdDtlDTO> prjPrdDtlDTOs = this.projectApiService.selectPrjPrdDtlList(prjPrdDtlQc);
				if(CollectionUtils.isNotEmpty(prjPrdDtlDTOs)){
					PrjPrdDtlDTO prjPrdDtlDTO = prjPrdDtlDTOs.get(0);
				//项目产品ID
					prjBrCrLimRecDTO.setPrjPrdId(prjPrdDtlDTO.getPrjPrdId());
				//项目产品明细ID
					prjBrCrLimRecDTO.setPrjPrdDtlId(prjPrdDtlDTO.getId());
				}
				
				
			}
		}
		return page;
	}

	@Override
	public boolean addBrCrLitRecList(PrjBrCrLimRecQC qc) throws BaseException {
		return this.brCrLitApiService.addBrCrLitRecList(qc);
	}
	
	@Override
	public List<KeyValueVO> getLimTypCdList() throws BaseException {
		String code = DictionaryConstant.CTRL_PRJ_CRD_CHG_TYPE;
		return this.getKeyValueList(code);
	}
	
	
	public List<KeyValueVO> getKeyValueList(String code) throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApiService
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), code);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
}
